using UnityEngine;
using System.Collections;

public class PlayerInformations : MonoBehaviour 
{
    public Texture heart1;
    public Texture heart2;
    public Transform Player;
    public GameObject pauseContainer;
    private static int points = 0;
	private static int life;
    private bool dead = false;
    private string playerName = "";

	void Start()
	{
        points = 0;
		life = 2;
		dead = false;

        Time.timeScale = 1;
	}

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Escape))
        {
            pauseContainer.SetActive(!pauseContainer.activeSelf);
            Time.timeScale = 1 - Time.timeScale;
        }
    }

    public static void AddPoints(int points)
    {
        PlayerInformations.points += points;
    }

    public static void RemoveLife()
    {
        life--;
    }

    public static int Life { get { return life; } }

	void OnGUI()
	{
        GUI.Label(new Rect(Screen.width - 110, 10, Screen.width / 4, Screen.height / 6), "Pontos : " + points);	
		
		if(life == 2)
		{
			GUI.DrawTexture(new Rect(10,10, 30, 30), heart1);
			GUI.DrawTexture(new Rect(40,10, 30, 30), heart1);
		}
		
		if(life == 1)
		{
			GUI.DrawTexture(new Rect(10,10, 30, 30), heart1);
			GUI.DrawTexture(new Rect(40,10, 30, 30), heart2);
		}
		
		if(life == 0)
		{
			GUI.DrawTexture(new Rect(10,10, 30, 30), heart2);
			GUI.DrawTexture(new Rect(40,10, 30, 30), heart2);
			
		}
		
		if(life < 0)
		{
			dead = true;
		}
			
		if(dead)
		{
			GUI.Label(new Rect(Screen.width/2 + 10,Screen.height/2, Screen.width / 4, Screen.height / 6), "Pontos : "+ points);
            playerName = GUI.TextField(new Rect(30, Screen.height / 2, Screen.width / 4, Screen.height / 10), playerName, 10);

            if(GUI.Button(new Rect(Screen.width / 2, Screen.height / 2 + Screen.height / 6, Screen.width / 4, Screen.height / 6), "Retry"))
			{
				dead = false;
				Application.LoadLevel("Game");
			}

            if(GUI.Button(new Rect(30, Screen.height / 2 + Screen.height/6, Screen.width / 4, Screen.height / 6), "Submit"))
            {
                if(playerName != "")
                {
                    RecordManager.NewRecord(points, playerName);
                    Application.LoadLevel("Highscore");
                }
            }

            if(Player != null)
            {
                Destroy(Player.gameObject);
            }
		}
	}
}